package chef.myapplication.model;

//import eu.hansolo.tilesfx.tools.TreeNode;

import chef.myapplication.Main;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.File;
import java.util.ArrayList;
import java.util.Objects;

public class FileTree {
    private static File currentFile;

    private static final ArrayList<File> imageFiles =new ArrayList<>();//同一目录下的图片
    private final TreeView<FileTreeNode> treeView;
    /*
    construct 构造方法
     */
    private  Image FileImage;
    public FileTree(TreeView<FileTreeNode> treeView){
        this.treeView=treeView;
        initFileTree();
    }

    public Image getFileImage() {
        return FileImage;
    }

    /**
     * 初始化文件树
     */
    private void initFileTree(){

        TreeItem<FileTreeNode> root = new TreeItem<>(new FileTreeNode(new File("我的电脑")));
        ImageView imageView =new ImageView(Objects.requireNonNull(Main.class.getResource("icon/我的电脑.png")).toString());
        imageView.setPreserveRatio(true);
        imageView.setFitWidth(22);
        FileImage  =new Image(Objects.requireNonNull(Main.class.getResource("icon/文件.png")).toString());
        root.setGraphic(imageView);
        root.setExpanded(true);
        treeView.setRoot(root);
        File[] rootPath = File.listRoots();
        for (File file : rootPath) {
            root.getChildren().add(buildFileSystemBrowser(new FileTreeNode(file)));
        }
    }
    /*
    get treeView
     */
    public TreeView<FileTreeNode> getTreeView(){

        return this.treeView;
    }

    public static File getCurrentFile() {
        return currentFile;
    }

    public static void setCurrentFile(File currentFile) {
        FileTree.currentFile = currentFile;
    }

    public static ArrayList<File> getImageFiles() {
        return imageFiles;
    }

    /**
     创建文件浏览器
     */
    private TreeItem<FileTreeNode> buildFileSystemBrowser(FileTreeNode treeNode) {
        return  createNode(treeNode);
    }



    /**
     创建文件子节点
     */
    private TreeItem<FileTreeNode> createNode(final FileTreeNode treeNode) {
        ImageView imageView = new ImageView(FileImage);
        imageView.setFitWidth(30);
        imageView.setPreserveRatio(true);
        return new TreeItem<>(treeNode, imageView) {

            private boolean isLeaf;
            private boolean isFirstTimeChildren = true;
            private boolean isFirstTimeLeaf = true;

            @Override
            public ObservableList<TreeItem<FileTreeNode>> getChildren() {
                if (isFirstTimeChildren) {
                    isFirstTimeChildren = false;
                    super.getChildren().setAll(buildChildren(this));
                }
                return super.getChildren();
            }

            @Override
            public boolean isLeaf() {

                if (isFirstTimeLeaf) {
                    isFirstTimeLeaf = false;
                    FileTreeNode fileTreeNode = getValue();
                    File f = fileTreeNode.getFile();
                    isLeaf = f.isFile();
                }
                return isLeaf;
            }

            private ObservableList<TreeItem<FileTreeNode>> buildChildren(TreeItem<FileTreeNode> TreeItem) {
                File f = TreeItem.getValue().getFile();
                if (f != null && f.isDirectory()) {
                    // private final String[] target = {"jpg","png","gif"};
                    File[] files = f.listFiles(File::isDirectory);
                    if (files != null) {
                        ObservableList<TreeItem<FileTreeNode>> children = FXCollections.observableArrayList();
                        for (File childFile : files) {
                            if (childFile != null)
                                children.add(createNode(new FileTreeNode(childFile)));
                        }
                        return children;
                    }
                }
                return FXCollections.emptyObservableList();
            }
        };

    }

    public static class FileTreeNode {
        private String fileName;
        private final File file;

        public FileTreeNode(File file){
            this.file=file;
            fileName=file.getName();
            if(fileName.equals(""))
                fileName=file.getPath();
        }
        public File getFile(){
            return file;
        }
        @Override
        public String toString(){
            return fileName;
        }
    }
}

